#include "stdio.h"
#include <stdint.h>
#include "../section_locate.h"
#include "pt-thread.h"
#include "my_utils.h"


static void hello_all(void);
PLACE_FUNC_AT_SEC(hello_all);

#include <string.h>

//#include <stdarg.h>
#include <string.h>
#define PRINTF_MAX	128
char app_printf_buf[PRINTF_MAX];

typedef char *my_va_list;
//
//typedef int32_t acpi_native_int;
//#define  _AUPBND                (sizeof (acpi_native_int) - 1)      // 就是3
//#define  _ADNBND                (sizeof (acpi_native_int) - 1)
//typedef char *va_list;
//#define _bnd(X, bnd)            (((sizeof (X)) + (bnd)) & (~(bnd)))
//#define va_arg(ap, T)           (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
//#define va_end(ap)              (ap = (va_list) NULL)
//#define va_start(ap, A)         (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))





// 对size求"进4"法, 即1~3都算4, 5~7都算8
static inline size_t get_4B_size(size_t size)
{
	size_t tmp = size % 4;
	if (tmp > 0){	// 不是4的倍数
		return (size / 4) * 4 + 4;
	}else{			// 正好是4的倍数
		return size;
	}

}

// 获取变量A后面的地址赋值给ap
#define my_va_start(ap, A)	  ( (ap) = ( ((char *) &(A)) + get_4B_size(sizeof(A)) ) )
                                        // 获取A的地址		// 偏移到A的下一个

// 获取参数列表中当前参数的值, 并把列表指针指向下一个参数
#define my_va_arg(ap, T)    \
({ \
    T   arg_val;    \
    arg_val = *(T *)ap;/*获取当前参数的值*/ \
    ap += get_4B_size(sizeof(T)); /*指向下一个参数*/\
    arg_val;/*GNU语句表达式的最后一个表达式的值为该宏的返回值*/    \
})

#define my_va_end(ap)              (ap = (va_list) NULL)

#if 0
void *p_fmt;
static void app_printf ( const char * fmt, ... )
{
    (void)fmt;

    p_fmt = (void *)&fmt;
	static void * vlist;				// 省略号...中的第一个参数的地址
//	my_va_list vlist;
	my_va_start(vlist, fmt);

	static char param_a;
	param_a = my_va_arg(vlist, char);	// 第1个传入的参数是char型

	static int param_b;
	param_b = my_va_arg(vlist, int);	// 第2个传入的参数是int型

	static long long param_c;
	param_c = my_va_arg(vlist, long long);	// 第3个传入的参数是long long型

	static double param_d;
	param_d = my_va_arg(vlist, double);

}
#endif // 0




#ifdef __cplusplus
#define auto_t auto
#else
#define auto_t  __auto_type
#endif // __cplusplus

typedef struct asdf_s{
    short a;
//    double b;
//    char c;
}asdf_t;
//DEFINE_ARRAY_STR(asdf_t);



void pass_arr(array_t p_a)
{
    auto_t p = (array_type(struct asdf_s))p_a;(void)p;
//    array_type(struct asdf_s) p = (__typeof(p)) p_a ;

//    struct asdf_s tmp = {
//        .a = 9,
//        .b = 1.3,
//        .c = 88,
//    };
//
//    array_fill(p, tmp);
//    for(array_itor(p) it = array_begin(p); it != array_end(p) ; it++){
//        printf("it: %p, *it: %f\n", it, it->b);
//    }

}




DEFINE_VECTOR(my_vec, struct asdf_s);
DEFINE_VECTOR_INIT(my_vec2, int, {7, 6, 4, 8, 10} );
#include "sort.h"


#include "algorithm_collector.h"
#include <assert.h>






static void hello_all(void)
{
    DEFINE_DEQUE(my_deq, int);
//    deq_push_back(my_deq, 88);


//    for(int i = 0; i < 24; i++){
//        deq_push_back(my_deq, i * 99);
//    }
    deq_resize(my_deq, 12);
    printf("size: %d\n", deq_size(my_deq));
    deq_front(my_deq) = 99;
    printf("deq_front: %d\n", deq_front(my_deq));
    deq_back(my_deq) = 55;
    printf("deq_back: %d\n", deq_back(my_deq));
//    for(int i = 0; i < 24; i++){
//        deq_push_back(my_deq, i * 99);
//    }

//    for(int i = 0; i < 36; i++){
//        int aa = deq_pop_front(my_deq);
//        printf("aa: %d\n", aa);
//
//    }

    for(auto_t it = deq_begin(my_deq); it != deq_end(my_deq); it++){\
        printf("*it : %d\n", *it);
    }


    deq_clear(my_deq);
    printf("size: %d\n", deq_size(my_deq));
    printf("cap: %d\n", deq_capacity(my_deq));
    deq_shrink_to_fit(my_deq);
    printf("size: %d\n", deq_size(my_deq));
    printf("cap: %d\n", deq_capacity(my_deq));

    #if 0
    DEFINE_ARRAY(my_arr2, double, 18);

    array_fill(my_arr2, 88);

    for(array_itor(my_arr2) it = array_begin(my_arr2); it != array_end(my_arr2) ; it++){
        printf("it: %p, *it: %f\n", it, *it);
    }



    DEFINE_ARRAY(my_arr1, struct asdf_s, 8);
    struct asdf_s tmp = {
        .a = 9,
        .b = 1.2,
        .c = 88,
    };

    array_fill(my_arr1, tmp);
    for(array_itor(my_arr1) it = array_begin(my_arr1); it != array_end(my_arr1) ; it++){
        printf("it: %p, *it: %f\n", it, it->b);
    }

    pass_arr(my_arr1);

    DEFINE_ARR2D(my_2d, int, 5, 5);

    arr2d_fill(my_2d, 77);

    #endif // 0


}





